package com.flot.yazb.base.model;

import android.content.Context;

import com.flot.yazb.base.client.RetrofitFactory;
import com.flot.yazb.aop.stub.RMCallback;
import com.flot.yazb.aop.stub.RMResponse;
import com.flot.yazb.aop.stub.RMResult;
import com.flot.yazb.retrofit.service.BService;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;

/**
 * Created by Chexiangjia-MAC on 17/6/26.
 */

public class BModel<T extends Serializable> extends RMResponse implements Serializable, RMResult {


    public abstract static class Transit<bT extends Serializable> {
        private Context mContext = null;

        public Context getContext() {
            return mContext;
        }

        public abstract void onBorn(bT serializable, String code, String message);

        public Transit(Context context) {
            this.mContext = context;
        }
    }

    public static void getInstall(String id, final BModel.Transit<BModel> incubator) {
        RetrofitFactory.getInstance()
                .create(BService.UserService.class)
                .getinstall(id)
                .enqueue(new RMCallback<RMResponse<BModel>>(incubator.getContext()) {
                    @Override
                    protected void onSuccess(RMResponse<BModel> result) {
                        incubator.onBorn(result.content, result.code(), result.message());
                    }

                    @Override
                    protected void onFail(String errorCode, String errorInfo) {
                        incubator.onBorn(null, errorCode, errorInfo);
                    }
                });
    }

    public Class getTClass() {
        Class tClass = (Class) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
        return tClass;
    }

}
